package com.seu.me.test;

import com.alibaba.fastjson.JSONObject;
import com.seu.me.utils.MD5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.io.FileOutputStream;

/**
 * Created by liangfeihu on 2018/3/26.
 */
@Slf4j
public class MyTest3 {

    public static void main(String[] args) {
        /*String submitUrl = "http://xyjk-app-xjbk-frontend.dev.cs.jisuqianbao.com/interface-xjbk/login";
        String registerFrom = "xianjinbus_xyjk";

        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("maskType", "0");
        map.add("registerFrom", registerFrom);
        map.add("phoneNo", "15214353029");

        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(map, headers);

        ResponseEntity<JSONObject> jsonEntity = restTemplate.postForEntity(submitUrl, entity, JSONObject.class);
        JSONObject body = jsonEntity.getBody();
        System.out.println(JSONObject.toJSONString(jsonEntity.getBody()));*/

        /*
        String md5Sign = "cashbusbuscash";
        String urlPrefix = "http://cashbus-gaia.test.cashbus.com";
        RestTemplate restTemplate = new RestTemplate();

        // 通话详单
       String userPhoneCallRecordsUrl = urlPrefix + "/getUserPhoneCallRecords?phone=15214353029&mtk=" + MD5.md5("15214353029", md5Sign);
        JSONArray phoneCallRecords = restTemplate.getForObject(userPhoneCallRecordsUrl, JSONArray.class);
        //System.out.println("phoneCallRecords:" + phoneCallRecords.toJSONString());

        // 短信详单
        String userPhoneSmsRecordsUrl = urlPrefix + "/getUserPhoneSmsRecords?phone=15214353029&mtk=" + MD5.md5("15214353029", md5Sign);
        //JSONArray phoneSmsRecords = restTemplate.getForObject(userPhoneSmsRecordsUrl, JSONArray.class);
        //String phoneSmsRecords = restTemplate.getForObject(userPhoneSmsRecordsUrl, String.class);
        JSONObject phoneSmsRecords = restTemplate.getForObject(userPhoneSmsRecordsUrl, JSONObject.class);
        //System.out.println("phoneSmsRecords:" + phoneSmsRecords);

        // 流量详单
        String userPhoneFlowRecordsUrl = urlPrefix + "/getUserPhoneFlowRecords?phone=15214353029&mtk=" + MD5.md5("15214353029", md5Sign);
        JSONArray phoneFlowRecords = restTemplate.getForObject(userPhoneFlowRecordsUrl, JSONArray.class);
        //System.out.println("phoneFlowRecords:" + phoneFlowRecords.toJSONString());

        String userPhoneMonthBillsUrl = urlPrefix + "/getUserPhoneMonthBills?phone=15214353029&mtk=" + MD5.md5("15214353029", md5Sign);
        JSONArray phoneMonthBills = restTemplate.getForObject(userPhoneMonthBillsUrl, JSONArray.class);

        String userPhoneInfoUrl = urlPrefix + "/getUserPhoneUserInfo?phone=15214353029&mtk=" + MD5.md5("15214353029", md5Sign);
        JSONObject userPhoneInfo = restTemplate.getForObject(userPhoneInfoUrl, JSONObject.class);

        JSONObject object = new JSONObject();
        object.put("phoneCallRecords", phoneCallRecords); // 短信详单
        object.put("phoneSmsRecords", phoneSmsRecords); // 短信详单
        object.put("phoneFlowRecords", phoneFlowRecords); // 流量详单
        object.put("phoneMonthBills", phoneMonthBills);
        object.put("userPhoneInfo", userPhoneInfo);
        //System.out.println("phone:" + object.toJSONString());

        writeStringToFile5("phoneInfo2.txt", object.toJSONString());*/

        String submitUrl = "https://open-test.qianhaishuliang.com/promotion/acceptance/yrjr";
        String testKey = "lghjgfg";

        RestTemplate restTemplate = new RestTemplate();

        Long timestamp = (long) System.currentTimeMillis() / 1000;
        JSONObject object = new JSONObject();

        object.put("sign", MD5.MD5Encode(testKey + timestamp));
        object.put("timestamp", timestamp);
        object.put("phoneMd5", MD5.MD5Encode("15214353029"));


        JSONObject jsonEntity = restTemplate.postForObject(submitUrl, object, JSONObject.class);
        String code = jsonEntity.getString("code");
        Boolean accept = jsonEntity.getBoolean("accept");

        System.out.println(JSONObject.toJSONString(jsonEntity));
        System.out.println(code + " : " + accept);


    }

    public static void writeStringToFile5(String filePath, String object) {
        try {
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(object.getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
